package com.equipment;

import java.util.NoSuchElementException;

import android.util.Log;

import com.util.EncodingUtil;

/**
 * B8指令释放OBU
 * @version 1.18
 * @author 刘亚东
 *
 */
public class CloseOBUConnectionResponse {

	private String STX = "55AA"; // 帧开始标志
	private String RSCTL = ""; // 串口帧序列号
	private String Length = ""; // 数据长度
	private String code = ""; // 代码
	private String broadening = ""; // 扩展代码
	private String Status = ""; // 0表示成功,非0表示失败
	private String BCC = ""; // 异或校验值

	/**
	 * 解析B8指令
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public CloseOBUConnectionResponse analysiss(String context)
			throws NoSuchElementException {
		CloseOBUConnectionResponse ResponseB8 = null;
		Log.e("COntext", context+"");
		// 判断是否是BB指令
		if (!context.substring(12, 14).toUpperCase().equals("B8")) {
			throw new NoSuchElementException("释放OBU错误！");
		}
		//55AA060002DFB80063
		// 判断状态是否成功
		if (!context.substring(14, 16).equals("00")) {
			throw new NoSuchElementException("释放OBU错误！");
		}
		// 截取到BCC校验值 需要判断校验是否相等
		BCC = context.substring(context.length() - 2).toUpperCase();
		// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)
		String str = context.substring(4, context.length() - 2);
		// 得到比对校验
		str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
		// 判断校验是否一致
		if (!str.equals(BCC)) {
			throw new NoSuchElementException("释放OBU异或错误!");
		}
		ResponseB8 = new CloseOBUConnectionResponse();
		// 解析串口帧序列号
		RSCTL = context.substring(4, 6);
		ResponseB8.setRSCTL(RSCTL);
		// 解析长度
		Length = context.substring(6, 10);
		ResponseB8.setLength(Length);
		// 解析代码类型
		code = context.substring(10, 12);
		ResponseB8.setCode(code);
		// 解析拓展子代码
		broadening = context.substring(12, 14);
		ResponseB8.setBroadening(broadening);

		return ResponseB8;
	}

	public String getSTX() {
		return STX;
	}

	public void setSTX(String sTX) {
		STX = sTX;
	}

	public String getRSCTL() {
		return RSCTL;
	}

	public void setRSCTL(String rSCTL) {
		RSCTL = rSCTL;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getBroadening() {
		return broadening;
	}

	public void setBroadening(String broadening) {
		this.broadening = broadening;
	}

	public String getStatus() {
		return Status;
	}

	public void setStatus(String status) {
		Status = status;
	}

	public String getBCC() {
		return BCC;
	}

	public void setBCC(String bCC) {
		BCC = bCC;
	}


}
